package com.bestcem.xm.componet.sms.push.core;

import com.bestcem.xm.componet.sms.push.PushSmsProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

import java.util.concurrent.TimeUnit;

@Slf4j
@Data
@RequiredArgsConstructor
@Component
@ConditionalOnProperty(prefix = "sms", name = "smsType", havingValue = "push")
public class PushSmsServiceFactory {

    private final PushSmsProperties pushSmsProperties;

    private final RedisTemplate<String, Object> redisTemplate;

    private final ObjectMapper mapper;

    public PushSmsSendApi createSendService() {
        //拦截器添加component_code
        GetRegisterInterceptor getRegisterInterceptor = new GetRegisterInterceptor(this.createCommonService(), this.pushSmsProperties, this.redisTemplate, this.mapper);
        OkHttpClient.Builder builder = this.getOkHttpClientBuilder(getRegisterInterceptor);

        OkHttpClient client = builder.build();
        Retrofit retrofit =
                new Retrofit.Builder().baseUrl(pushSmsProperties.getBaseSetting().getBaseUrl())
                        .client(client)
                        .addConverterFactory(BasicTypeConverterFactory.create())
                        .addConverterFactory(JacksonConverterFactory.create())
                        .addCallAdapterFactory(BodyCallAdapterFactory.create())
                        .addCallAdapterFactory(ResponseCallAdapterFactory.create())
                        .build();
        return retrofit.create(PushSmsSendApi.class);
    }

    public PushSmsCommonApi createCommonService() {
        OkHttpClient.Builder builder = this.getOkHttpClientBuilder(null);

        OkHttpClient client = builder.build();
        Retrofit retrofit =
                new Retrofit.Builder().baseUrl(pushSmsProperties.getBaseSetting().getBaseUrl())
                        .client(client)
                        .addConverterFactory(BasicTypeConverterFactory.create())
                        .addConverterFactory(JacksonConverterFactory.create())
                        .addCallAdapterFactory(BodyCallAdapterFactory.create())
                        .addCallAdapterFactory(ResponseCallAdapterFactory.create())
                        .build();
        return retrofit.create(PushSmsCommonApi.class);
    }

    private OkHttpClient.Builder getOkHttpClientBuilder(Interceptor myInterceptor) {
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
        builder.readTimeout(pushSmsProperties.getBaseSetting().getReadTimeout() == null ? 3 : pushSmsProperties.getBaseSetting().getReadTimeout(), TimeUnit.SECONDS);
        builder.connectTimeout(pushSmsProperties.getBaseSetting().getConnectTimeout() == null ? 5 : pushSmsProperties.getBaseSetting().getConnectTimeout(), TimeUnit.SECONDS);
        if (myInterceptor != null) {
            builder.addInterceptor(myInterceptor);
        }
        //日志,方便调试
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(interceptor);
        return builder;
    }
}
